package com.example.desinger.designPattern.dutymodel;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.desinger.common.R;
import com.example.desinger.entity.HomeRemark;
import com.example.desinger.entity.StuHomeWork;
import com.example.desinger.service.StuHomeWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HomeworkExistenceHandler extends AbstractHandler {
    @Autowired
    private StuHomeWorkService stuHomeWorkService;

    public HomeworkExistenceHandler(StuHomeWorkService stuHomeWorkService) {
        this.stuHomeWorkService = stuHomeWorkService;
    }

    @Override
    public R<String> handle(HomeRemark homeRemark) {
        LambdaQueryWrapper<StuHomeWork> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StuHomeWork::getClassId, homeRemark.getClass_id())
                .eq(StuHomeWork::getStuId, homeRemark.getStu_id())
                .eq(StuHomeWork::getId, homeRemark.getStu_home_work_id());
        StuHomeWork one = stuHomeWorkService.getOne(lambdaQueryWrapper);
        if (one == null) {
            return R.error("数据库中没有这个同学的作业");
        } else if (nextHandler != null) {
            return nextHandler.handle(homeRemark);
        }
        return R.error("未知错误");
    }
}

